package com.dididoctor.patient.Receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Empty.EmptyActivity;
import com.dididoctor.patient.Activity.Evaluate.EvaluateActivity;
import com.dididoctor.patient.Activity.Inquiry.InquiryActivity;
import com.dididoctor.patient.Bean.DiaeasesLitapal;
import com.dididoctor.patient.Dialog.DiseaseListDialog;
import com.dididoctor.patient.Utils.MyToast;

import java.util.ArrayList;
import java.util.List;

/**
 * XMPP 退出登录广播
 *
 * @author nseer
 */
public class xmppReceier extends BroadcastReceiver implements xmppView {
    private Context context;

    private String hosId, recId, name;
    private String userid, token;
    private xmppPresenter presenter;
    private String doctorid, accId, sign;
    private List<DiaeasesLitapal> litapals = new ArrayList<>();
    @Override
    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        presenter = new xmppPresenter(context);

        String action = intent.getAction();
        if (ConstantValue.WYYX.equals(action)) {
            String msgType = intent.getStringExtra("msgType");
            recId = intent.getStringExtra("recId");
            hosId = intent.getStringExtra("hosId");
            userid = intent.getStringExtra("userid");
            token = intent.getStringExtra("token");
            doctorid = intent.getStringExtra("doctorid");
            accId = intent.getStringExtra("accId");
            name = intent.getStringExtra("name");
            sign = intent.getStringExtra("sign");
            switch (msgType) {
                case "END":
                    presenter.getgoAndFrom(recId, hosId, userid, token, doctorid, accId, name, sign);
                    break;
                case "CONSULT":

//                    Intent intent1 = new Intent(context, EmptyActivity.class);
//                    intent1.putExtra("recId", recId);
//                    intent1.putExtra("hosId", hosId);
//                    intent1.putExtra("userid", userid);
//                    intent1.putExtra("token", token);
//                    intent1.putExtra("doctorid", doctorid);
//                    intent1.putExtra("accId", accId);
//                    intent1.putExtra("name", name);
//                    intent1.putExtra("sign", sign);
//                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.startActivity(intent1);
                    //24z中小病
                    litapals = DiaeasesLitapal.getdisarases();
                    DiseaseListDialog diseaseListDialog = new DiseaseListDialog(context, litapals, new DiseaseListDialog.onItemClickListener() {
                        @Override
                        public void itemClick(int position) {
                            DiaeasesLitapal litapal = litapals.get(position);
                            String disname = litapal.getName();
                            String disid = litapal.getDisId();
                            Intent intent = new Intent(context, InquiryActivity.class);
                            intent.putExtra("disname", disname);
                            intent.putExtra("disid", disid);
                            intent.putExtra("doctorId", doctorid);
                            intent.putExtra("hosId", hosId);
                            intent.putExtra("isCmp", "1");
                            intent.putExtra("accId", accId);
                            intent.putExtra("sign", sign);
                            intent.putExtra("name", name);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
//                            diseaseListDialog.dismiss();
                        }

                        @Override
                        public void setfinis() {

                        }

//                        @Override
//                        public void setfinis() {
//                            finish();
//                        }
                    });
                    diseaseListDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  //窗口可以获得焦点，响应操作
                    diseaseListDialog.show();

                    break;
                case "TWOEND":
                    Intent intent1 = new Intent(context, EvaluateActivity.class);
                    intent1.putExtra("recId", recId);
                    intent1.putExtra("hosId", hosId);
                    intent1.putExtra("userid", userid);
                    intent1.putExtra("token", token);
                    intent1.putExtra("doctorid", doctorid);
                    intent1.putExtra("accId", accId);
                    intent1.putExtra("name", name);
                    intent1.putExtra("iszq", true);
                    intent1.putExtra("sign", sign);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(intent1);
//                    presenter.getgoAndFrom(recId, hosId, userid, token, doctorid, accId, name, sign);
                    break;
            }
        }
    }


    @Override
    public void showToastMessage(String message) {

    }

    @Override
    public void showLoadingDialog(String message) {

    }

    @Override
    public void cancelLoadingDialog() {

    }

    @Override
    public void onProgress(int bytesWritten, int totalSize) {

    }

    @Override
    public void isOK() {

//                    EvaluateActivity.start(context,hosId,recId);
    }

    @Override
    public void isBall() {
        MyToast.DefaultmakeText(context, "结束会话失败", 0);

    }
}
